<?php
include_once(APP_PATH."/sms/global.php");
class sms_plugin_whatsnew{
	/**
	 * @param sms_controller_index $controller
	 */
	
	private static $obj;
	private static $new_sms_num;
	
	/**
	 * 初始化静态属性
	 *
	 * @param unknown_type $controller
	 */
	public static  function init($controller){
		if(!self::$obj){
			self::$obj =  Model::factory('sms_model_data');
		}
		if(!self::$new_sms_num){
			self::$new_sms_num = self::$obj->queryUnReadSms(Controller::$login['uid']);
		}
	}
	public static function getNewMessage($controller){
		self::init($controller);
		return $controller->newMessage = self::$new_sms_num['smsnum']>=0?self::$new_sms_num['smsnum']:0;
	}
	
	/**
	 * @param sms_controller_index $controller
	 */
	public static function getNewSystem($controller){
		self::init($controller);
		return $controller->newSystem = self::$new_sms_num['sysnum']>=0?self::$new_sms_num['sysnum']:0;
	}
	
	/**
	 * @param sms_controller_index $controller
	 */
	public static function getNewAnnouncement($controller){
		$o = Model::factory("sms_model_annlog");
		return $controller->newAnnouncement = $o->getUnread(Controller::$login['uid'], 0);
	}
	
	public static function setReadMessage($controller){
		self::init($controller);
		if(self::$obj->cleanSmsCount(Controller::$login['uid'])){
			self::$obj->setReaded(Controller::$login['uid'],true);
			CacheManager::getInstance()->refresh('sms');
		}
/*		self::$obj->setReaded(Controller::$login['uid'],true);
		if($affected){
			CacheManager::getInstance()->refresh('sms');
		}*/
		return ;
	}
	
	public static function setReadSystem($controller){
		self::init($controller);
		if(self::$obj->cleanSysSmsCount(Controller::$login['uid'])){
			self::$obj->setReaded(Controller::$login['uid'],false);
			CacheManager::getInstance()->refresh('sms');
		} 
		/*self::$obj->setReaded(Controller::$login['uid'],false);
		CacheManager::getInstance()->refresh('sms');
		if($affected>0){
			CacheManager::getInstance()->refresh('sms');
		}*/
		return;
	}
	
	public static function setReadAnnouncement($controller){
		$uid = Controller::$login['uid'];
		$o = Model::factory("sms_model_annlog");
		$unreads = $o->getUnreadIds($uid);
		$a = array();
		foreach($unreads as $unread){
			array_push($a, array('uid'=>$uid, 'ann_id'=>$unread['id']));
		}
		if(count($a)>0){
			$o->add($a);
			CacheManager::getInstance()->refresh('sms');
		}
	}
	/**
	 * 更新未读短消息数量
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type
	 * @param unknown_type $num
	 * @return unknown
	 */
	public static function setNewMsgCount($uid,$type,$num=1){
		if(!($o=self::$obj)){
			$o = Model::factory('sms_model_data');
		}
		return $o->updateSmsCount($uid,$type,$num);
	}
	
	/**
	 * 更新评论未读短消息数
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $num
	 * @return unknown
	 */
	public static function setNewCommenCount($uid,$num=1){
		return self::setNewMsgCount($uid,sms_config::MSG_COMMENT_CNT,$num);
	}
	/**
	 * 更新评论未读短消息数
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $num
	 * @return unknown
	 */
	public static function resetNewCommenCount($uid){
		return self::setNewMsgCount($uid,sms_config::MSG_COMMENT_CNT,0);
	}
	
	/**
	 * 转帖短信提醒
	 * Enter description here ...
	 * @param unknown_type $controller
	 */
	public static function sendCiteNotice_old($controller){
		$uid = Controller::$login['uid'];
		if(!($uid && is_numeric($uid))) return false;
		$key = CACHE_KEY_NEW_CITE_PRE.$uid;
		include_once DOCUMENT_PATH.'/api/api_nga/sdk/API_Client.php';
		$difftime = intval(time()-$cache['time']);
		if(!($cache = MemRow::getCache($key)) || time()-$cache['time']>CACHE_EXPIRY_TIME ){
			//如果没有cache，新建cache
			//没有发送或者已经过期
			if($notice->isCiteNotice($uid)){
				$api = new API_Client();//调用动态接口，获取好友转给他的转贴。
				if($feed = $api->feed_get($uid, 10, 10)){
					$str = '<div class="sms_feed"><h3>您的好友转帖给您：</h3><div class="sms_feed_h3_right"><a href="#" onclick="refuseCiteNotice();">不再接收此类消息</a><ul>';
					foreach ($feed AS $v){
						$row = unserialize($v['feed_data']);
						$url = preg_replace('/(^[^\/].*)/i','/\\1',$row['url']);
						$str.='<li><a href="http://i.178.com/?uid='.$v['uid'].'" target="_blank" title="'.$v['username'].'">'.mb_substr($v['username'],0,4,'utf-8').'</a> 转帖给您：<span class="brackets-l-icon"></span><a href="'.$url.'" target="_blank" title="'.$row['title'].'" >'.mb_substr($row['title'],0,30,'utf-8').'</a><span class="brackets-r-icon"></span></li>';
					}
					$str.='</ul></div>';
					sms_util_system::send(NULL,$uid,$str,':nga:','1',time(),'i.178.com','今天您的好友给您的精彩转帖');
				}
			}
			/**
			 * 发送短信代码
			 */
			//跟新memcache数据
			MemRow::setCache($key, array('uid'=>$uid,'flag'=>1,'time'=>time()),0);	
		}
		return ;
	}
	
	/**
	 * 转帖短信提醒
	 * Enter description here ...
	 * @param unknown_type $controller
	 */
	public static function sendCiteNotice($controller){
		$uid = Controller::$login['uid'];
		if(!($uid && is_numeric($uid))) return false;
		if(self::ignorePlugin($controller->app, $controller->controller, $controller->action)) return false;
		$notice = Model::factory('sms_model_system');
		$key = CACHE_KEY_NEW_CITE_PRE.$uid;
		include_once DOCUMENT_PATH.'/api/api_nga/sdk/API_Client.php';
//		if(!($cache = MemRow::getCache($key)) || time()-$cache['time']>CACHE_EXPIRY_TIME ){
			if($notice->isCiteNotice($uid)){
			//如果没有cache，新建cache
			//没有发送或者已经过期
				$api = new API_Client();//调用动态接口，获取好友转给他的转贴。
				if($feed = $api->feed_get($uid, 10, 10)){
					$str = '<div class="sms_feed"><h3><div class="sms_feed_h3_right"><a href="#" onclick="refuseCiteNotice();">不再接收此类消息</a></div>您的好友转帖给您：</h3><ul> ';
					foreach ($feed AS $v){
						$row = unserialize($v['feed_data']);
						$url = preg_replace('/(^[^\/].*)/i','/\\1',$row['url']);
						$username = mb_strlen($v['username'],'utf-8')>4 ? mb_substr($v['username'],0,4,'utf-8').'…':$v['username'];
						$title = mb_strlen($v['title'],'utf-8')>30?mb_substr($row['title'],0,30,'utf-8').'…':$row['title'];
						$str.='<li><a href="http://i.178.com/?uid='.$v['uid'].'" target="_blank" title="'.$v['username'].'">'.$username.'</a> 转帖给您：<span class="brackets-l-icon"></span><a href="'.$url.'" target="_blank" title="'.$row['title'].'" >'.$title.'</a><span class="brackets-r-icon"></span></li>';
					}
					$str.='</ul></div>';
					/**
			 	 	* 发送短信代码
			 		*/
					sms_util_system::send(NULL,$uid,$str,':nga:','1',time(),'i.178.com','今天您的好友给您的精彩转帖');
				}
//				MemRow::setCache($key, array('uid'=>$uid,'time'=>time(),'flag'=>1), 0);
			}
//		}
		return ;
	}
	
	public static function ignorePlugin($app,$controller,$action){
		if($_SERVER['apps']['sms']['ignore']){
			foreach ($_SERVER['apps']['sms']['ignore'] as $v){
				if($app==$v[0] && $controller == $v[1] && $action == $v[2]){
					return true;
				}
			}
		}
		return false;
	}
	
//	public function clean…
}